//
//  FindPasswordController.m
//  sign up
//
//  Created by Sizhu Ba on 2017/8/28.
//  Copyright © 2017年 Sizhu Ba. All rights reserved.
//

#define kWidthOfScreen [UIScreen mainScreen].bounds.size.width
#define kHeightOfScreen [UIScreen mainScreen].bounds.size.height

#import "FindPasswordController.h"
#import "Masonry.h"
#import "MBProgressHUD+Ex.h"
@interface FindPasswordController ()
{
    NSInteger secondsCountDown;
    NSTimer *countDownTimer;
}
@property (nonatomic, assign) CGFloat viewHeight;
@property (nonatomic, strong) UITextField *phoneNumberTF;
@property (nonatomic, strong) UILabel *phoneL;
@property (nonatomic, strong) UIView *line;

@property (nonatomic, strong) UITextField *verificationCodeTF;
@property (nonatomic, strong) UILabel *verificationCodeL;
@property (nonatomic, strong) UIView *line2;

@property (nonatomic, strong) UIButton *getCodeBt;
@property (nonatomic, strong) UIButton *nextBt;


@end

@implementation FindPasswordController
-(void)loadView{
    
    [self setView];
}
//界面设置
-(void)setView {
    //背景
    self.view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kWidthOfScreen, kHeightOfScreen)];
    self.view.backgroundColor = [UIColor whiteColor];
    //self.view.backgroundColor = [UIColor colorWithRed:87/255.0 green:229/255.0 blue:237/255.0 alpha:1]; //设置背景为天蓝色
    self.viewHeight = kWidthOfScreen < 374.0 ? 150.0 : 200.0; //设置屏幕距离
    [self.view addSubview:self.phoneNumberTF];
    [self.view addSubview:self.phoneL];
    [self.view addSubview:self.verificationCodeL];
    [self.view addSubview:self.verificationCodeTF];
    [self.view addSubview:self.line];
    [self.view addSubview:self.line2];
    [self.view addSubview:self.getCodeBt];
    
    
    self.nextBt = [self setButton:CGRectMake(0, 0, 0, 0) andTitle:@"next" action:@selector(nextMethod)];
    [self.view addSubview:self.nextBt];
    [countDownTimer invalidate];
    [self setManory];
}

/**
 * 下一页
 */
-(void)nextMethod {
    NSLog(@"%@",@"下一页");
}
/**
 * 设置约束
 */
-(void)setManory {
    
    __weak typeof (self) weakSelf = self; //避免循环引用
    
    
    [self.phoneL mas_makeConstraints:^(MASConstraintMaker *make) {//Username
        make.top.equalTo(weakSelf.view.mas_top).with.offset(self.viewHeight);
        make.left.equalTo(weakSelf.view.mas_left).with.offset(20);
        
    }];
    
    [self.phoneNumberTF mas_makeConstraints:^(MASConstraintMaker *make) {//Username填写
        // make.top.equalTo(weakSelf.phoneL.mas_top);
        make.centerY.equalTo(weakSelf.phoneL.mas_centerY);
        make.left.equalTo(weakSelf.phoneL.mas_right).with.offset(20);
        make.width.mas_equalTo(@180);
        make.height.mas_equalTo(@30);
    }];
    
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {//下划线1
        make.top.equalTo(weakSelf.phoneL.mas_bottom).with.offset(5);
        make.left.equalTo(weakSelf.view.mas_left).with.offset(20);
        make.right.equalTo(weakSelf.view.mas_right).with.offset(-20);
        make.height.mas_equalTo(@1);
    }];
    
    
    
    
    
    [self.verificationCodeL mas_makeConstraints:^(MASConstraintMaker *make) {//codeLable
        make.top.equalTo(weakSelf.line.mas_top).with.offset(20);
        make.left.equalTo(weakSelf.view.mas_left).with.offset(20);
        
    }];
    
    [self.verificationCodeTF mas_makeConstraints:^(MASConstraintMaker *make) {//code填写
        make.top.equalTo(weakSelf.verificationCodeL.mas_top);
        make.left.equalTo(weakSelf.phoneNumberTF.mas_left);//.with.offset(20);
        //make.right.equalTo(weakSelf.view.mas_right).width.offset(-100);
    }];
    
    
    
    [self.getCodeBt mas_makeConstraints:^(MASConstraintMaker *make) {//获取验证码
        make.centerY.equalTo(weakSelf.verificationCodeL.mas_centerY);
        // make.left.equalTo(weakSelf.view.mas_left).with.offset(20);
        make.right.equalTo(weakSelf.view.mas_right).with.offset(-20);
        make.height.mas_equalTo(@25);
        if (kWidthOfScreen>374){
            make.width.mas_equalTo(@100);
        } else {
            make.width.mas_equalTo(@70);
        }
        
    }];
    
    [self.line2 mas_makeConstraints:^(MASConstraintMaker *make) {//下划线2
        make.top.equalTo(weakSelf.verificationCodeL.mas_bottom).with.offset(5);
        make.left.equalTo(weakSelf.view.mas_left).with.offset(20);
        make.right.equalTo(weakSelf.getCodeBt.mas_left).with.offset(-20);
        make.height.mas_equalTo(@1);
    }];
    
    [self.nextBt mas_makeConstraints:^(MASConstraintMaker *make) {//下一页按钮
        make.top.equalTo(weakSelf.getCodeBt.mas_bottom).with.offset(20);
        // make.left.equalTo(weakSelf.view.mas_left).with.offset(20);
        make.right.equalTo(weakSelf.view.mas_right).with.offset(-20);
        make.height.mas_equalTo(@30);
        make.width.mas_equalTo(@70);
    }];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.getCodeBt addTarget:self action:@selector(getCodeMethod) forControlEvents:UIControlEventTouchUpInside];
}
-(void)getCodeMethod {
    self.getCodeBt.backgroundColor = [UIColor lightGrayColor];
    [self.getCodeBt setTitle:@"60s" forState:UIControlStateNormal];
    [self.getCodeBt setEnabled:NO];
    //_getCodeBt.backgroundColor = [UIColor lightGrayColor];
    countDownTimer=  [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeFireMethod) userInfo:nil repeats:YES];
    secondsCountDown=60;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


-(void)timeFireMethod{
    secondsCountDown--;
    
    self.getCodeBt.backgroundColor = [UIColor lightGrayColor];
    [self.getCodeBt setTitle:[NSString stringWithFormat:@"%lds",(long)secondsCountDown] forState:UIControlStateNormal];
    
    if(secondsCountDown==0){
        
        [self.getCodeBt setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.getCodeBt setTitle:NSLocalizedString(@"Get Code", nil) forState:UIControlStateNormal];
        self.getCodeBt.backgroundColor = [UIColor redColor];
        self.getCodeBt.enabled = YES;
        [countDownTimer invalidate];
    }
}








#pragma mark - 懒加载
//输入的电话号码
-(UITextField *)phoneNumberTF {
    if (_phoneNumberTF  == nil) {
        _phoneNumberTF = [[UITextField alloc]initWithFrame:CGRectMake(0,0,0,0)];
        //_phoneNumber.backgroundColor = [UIColor orangeColor];
        _phoneNumberTF.borderStyle =  UITextBorderStyleNone ;
        _phoneNumberTF.placeholder = NSLocalizedString(@"Tel #", nil);//@"手机号";
        //_phoneNumber.secureTextEntry = YES; //密码格式
        //[_phoneNumberTF sizeToFit];
        if(kWidthOfScreen <374) {
            _phoneNumberTF.font = [UIFont systemFontOfSize:14.0];
        }
        
    }
    return _phoneNumberTF;
}
//账号lable
-(UILabel *)phoneL {
    if (_phoneL == nil) {
        _phoneL = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
        _phoneL.text =  NSLocalizedString(@"Cell Phone", nil);//@"手机号";

        _phoneL.hidden = NO;
        //[_phoneL sizeToFit];
        if(kWidthOfScreen <374) {
            _phoneL.font = [UIFont systemFontOfSize:14.0];
        }
    }
    return  _phoneL;
}
//下划线1
- (UIView *)line {
    if (_line == nil ) {
        _line = [[UIView alloc]initWithFrame:CGRectMake(18, self.viewHeight+kHeightOfScreen/33+kHeightOfScreen/33/3, kWidthOfScreen-36, 1)];
        _line.backgroundColor = [UIColor lightGrayColor];
    }
    return _line;
}


//codelable
-(UILabel *)verificationCodeL {
    if (_verificationCodeL == nil) {
        _verificationCodeL = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];// [[UILabel alloc]initWithFrame:CGRectMake(20, self.viewHeight+kHeightOfScreen/33+kHeightOfScreen/33/3+20, 40, kHeightOfScreen/33)];
        
        _verificationCodeL.text =  NSLocalizedString(@"Code", nil);//@"Code";
        _verificationCodeL.hidden = NO;
        if(kWidthOfScreen <374) {
            _verificationCodeL.font = [UIFont systemFontOfSize:14.0];
        }
    }
    return  _verificationCodeL;
}
//code
-(UITextField *)verificationCodeTF {
    if (_verificationCodeTF  == nil) {
        _verificationCodeTF = [[UITextField alloc]initWithFrame:CGRectMake(70, self.viewHeight+kHeightOfScreen/33+kHeightOfScreen/33/3+20, kWidthOfScreen-90, kHeightOfScreen/33)];
        //_phoneNumber.backgroundColor = [UIColor orangeColor];
        _verificationCodeTF.borderStyle =  UITextBorderStyleNone ;
        _verificationCodeTF.placeholder =  NSLocalizedString(@"Verification Code", nil);//请输入验证码
        //_verificationCodeTF.secureTextEntry = YES; //密码格式
        if(kWidthOfScreen <374) {
            _verificationCodeTF.font = [UIFont systemFontOfSize:14.0];
        }
    }
    return _verificationCodeTF;
}

//下划线2
- (UIView *)line2 {
    if (_line2 == nil ) {
        _line2 = [[UIView alloc]initWithFrame:CGRectMake(18, self.viewHeight+kHeightOfScreen/33*2+kHeightOfScreen/33/3+20+kHeightOfScreen/33/3, kWidthOfScreen-36, 1)];
        _line2.backgroundColor = [UIColor lightGrayColor];
    }
    return _line2;
}
//验证码
-(UIButton *)getCodeBt {
    if (_getCodeBt == nil) {
        _getCodeBt = [[UIButton alloc]initWithFrame:CGRectMake(0,0,40,30 )];
        
        //  _getCodeBt.backgroundColor = [[UIColor alloc]initWithRed:0/255 green:111/255 blue:255/255 alpha:1] ;
        _getCodeBt.backgroundColor = [UIColor redColor];
        [_getCodeBt setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];//设置文字颜色
        [_getCodeBt.layer setCornerRadius:5.0]; //设置矩形四个圆角半径
        [_getCodeBt setTitle:NSLocalizedString(@"Get Code", nil) forState:UIControlStateNormal];
        if(kWidthOfScreen <374) {
            _getCodeBt.titleLabel.font=[UIFont systemFontOfSize:14];
        }
        //_getCodeBt.enabled=NO;//设置不可点击
        //[_getCodeBt sizeToFit];
    }
    return _getCodeBt;
}


#pragma mark -Button
-(UIButton *)setButton:(CGRect)frame andTitle:(NSString *)title action:(SEL)action{
    
    UIButton *btn = [[UIButton alloc] initWithFrame:frame];
    if(kWidthOfScreen <374) {
        btn.titleLabel.font=[UIFont systemFontOfSize:14];
    }
    
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn setBackgroundColor:self.view.tintColor ];//forState:UIControlStateNormal];
    [btn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    [btn.layer setCornerRadius:5.0]; //设置矩形四个圆角半径
    return btn;
    
}
@end
